<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 100%; overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel"> 关闭 </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工地名称">
            <a-input placeholder="请输入工地名称" v-decorator.trim="['constructionName']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户">
            <a-select
              v-if="model.supplier != '客户'"
              placeholder="客户"
              :dropdownMatchSelectWidth="false"
              @change="handleSelectChange"
            >
              <a-select-option v-for="(item, index) in arrList" :key="index" :value="item.id">
                {{ item.supplier }}
              </a-select-option>
            </a-select>
            <a-col v-if="model.supplier == '客户'"><a-row>客户</a-row></a-col>
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开工时间">
            <j-date v-decorator="['projectStartTime']" :show-time="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="预计完成时间">
            <j-date v-decorator="['projectExpectedTime']" :show-time="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
            <a-input  v-decorator.trim="[ 'contacts' ]"  disabled />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
            <a-input  v-decorator.trim="[ 'telephone' ]" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电子邮箱">
            <a-input  v-decorator.trim="[ 'email' ]" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号码">
            <a-input  v-decorator.trim="[ 'phoneNum' ]" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="传真">
            <a-input  v-decorator.trim="[ 'fax' ]" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户信息">
            <a-input placeholder=""  v-model="kehuxx" disabled />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import {addEngineering,editSupplier,checkSupplier,findBySelectCus,updataProject } from '@/api/api'
  import JDate from '@/components/jeecg/JDate'
  export default {
    name: "projectListModal",
    components: {
      JDate
    },
    inject:["reload"],
    data () {
      return {
        ids:'',
        laixiren:'',
        lxdh:'',
        dzyx:'',
        sjhm:'',
        chuanz:'',
        kehuxx:'',
        arrList:[],
        title:"操作",
        visible: false,
        model: {},
        operTimeStr: '',
        isReadOnly: false,
        arr:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          supplier:{
            rules: [
              { required: true, message: '请输入名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
              { validator: this.validateSupplierName}
            ]
          }
        },
        url: {
          add: '/engineering/addEngineering',
          edit: '/depotHead/updateDepotHeadAndDetail',
          // edit: '',
          detailList: '/depotItem/getDetailList'
        }
      }
    },
    created () {
      this.SelectData()
    },
     mounted(){
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'constructionName', '', 'projectStartTime', 'projectExpectedTime',
            'contacts','telephone','email','phoneNum','fax'))
          // console.log(this.model,8787)
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          console.log(this.model,8888)
          console.log(values,9999)
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            console.log(this.model,)
            if(this.model.beginNeedGet && this.model.constructionName) {
              that.$message.warn("期初应收和期初应付不能同时输入");
              that.confirmLoading = false;
              return;
            }
            formData.supplierId = this.ids;
            let obj;
            if(!this.model.id){
              obj=addEngineering(formData);
            }else{
              obj=updataProject(formData);
            }
            obj.then((res)=>{
              if(res.code === 0){
                // that.$emit('成功');
                that.$message.success('成功');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
              this.reload();
            })
          }
        })
      },
      SelectData() {
            findBySelectCus({}).then((res)=>{
              // console.log(res,11)
              this.arrList = res
        })
      },
      // 下拉加载 请求数据
      handleSelectChange(record) {
        this.laixiren = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].contacts:''
        // this.lxdh = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].telephone:''
        // this.dzyx = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].email:''
        // this.sjhm = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].phoneNum:''
        // this.chuanz = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].fax:''
        // this.kehuxx = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].description:''
        this.ids = this.arrList.filter(item => item.id == record)[0]?this.arrList.filter(item => item.id == record)[0].id: ''
         console.log(record,this.arrList.filter(item => item.id == record))
        //  console.log(this.ids,999)
      },
      handleCancel () {
        this.close()
      },
      validateSupplierName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0
        };
        checkSupplier(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>
<style scoped>
.ant-calendar-picker {
  width: 250px;
}
</style>